<template>
  <el-dialog :title="info.title" :visible.sync="visible" append-to-body center width="460px" :before-close="handleCancel">
    <el-form ref="formDate" label-suffix=":" label-width="100px" :rules="rules" :model="form">
      <el-row>
        <el-col :span="24">
          <el-form-item label="转正日期" prop="visit_time">
            <el-date-picker
              v-model="form.visit_time"
              type="date"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              size="small"
              clearable
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="自我鉴定" prop="addr">
            <el-input v-model="form.addr" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" :rows="4" v-model="form.remark" size="small" clearable />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="fr mt10">
            <el-button size="small" @click="handleCancel()">取 消</el-button>
            <el-button type="primary" size="small" @click="handleEdit()">保 存</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { addProVisit, upProVisit } from '@/api/project'

export default {
  props: {
    info: {
      type: Object,
      default: null
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  data() {
    return {
      staffs: [],
      firsts: [{ key: 0, value: '否' }, { key: 1, value: '是' }],
      form: {},
      rules: {
        visit_time: [{ required: true, trigger: 'blur', message: '拜访时间不可为空' }],
        name: [{ required: true, trigger: 'blur', message: '联系人不可为空' }],
        phone: [{ required: true, trigger: 'blur', message: '联系电话不可为空' }],
        participants: [{ required: true, trigger: 'blur', message: '参加人员不可为空' }],
        content: [{ required: true, trigger: 'blur', message: '交流内容不可为空' }],
        addr: [{ required: true, trigger: 'blur', message: '地址不可为空' }],
        feedback: [{ required: true, trigger: 'blur', message: '客户反馈不可为空' }],
        schedule: [{ required: true, trigger: 'blur', message: '项目进度不可为空' }],
        note_taker: [{ required: true, trigger: 'blur', message: '记录人不可为空' }]
      },
      visible: false
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.form = this.info.edit
        if(this.info.type === 'add')this.$set(this.form, 'first', 0)
        if (this.info.type === 'edit') {
        }
      }
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  mounted() {
  },
  methods: {
    handleEdit() {
      this.$refs['formDate'].validate(async(valid) => {
        if (valid) {
          if (this.info.type === 'add') {
            addProVisit(this.form).then(response => {
              this.getData()
              this.handleCancel()
              this.$message.success('添加成功')
            }).catch(err => {
              this.$message.error(err.msg)
            })
          } else {
            upProVisit(this.form).then(response => {
              this.getData()
              this.handleCancel()
              this.$message.success('编辑成功')
            }).catch(err => {
              this.$message.error(err.msg)
            })
          }
        }
      })
    },
    handleCancel(done) {
      if (this.$refs['formDate']) this.$refs['formDate'].resetFields()
      if (done && typeof done === Function) {
        done()
      } else {
        this.visible = false
      }
    }
  }
}
</script>

<style>

</style>
